<?php

/**
 * This is the model class for table "surat_keluar".
 *
 * The followings are the available columns in table 'surat_keluar':
 * @property string $id_surat_keluar
 * @property string $no_surat
 * @property string $judul_surat
 * @property string $isi_surat
 * @property string $id_tipe_surat
 * @property string $id_pegawai
 * @property string $tanggal_pembuatan
 * @property integer $draft
 * @property integer $sort
 */
class SuratKeluar extends CActiveRecord
{
        
        public function getAcceptStatus()
        {
            return PersetujuanSuratKeluar::model()->count(
                                        'setuju=1 AND id_surat_keluar='.$this->id_surat_keluar
                                    ) ;
        }
        
        public function getRejectStatus()
        {
            return PersetujuanSuratKeluar::model()->count(
                                        'setuju=0 AND id_surat_keluar='.$this->id_surat_keluar
                                    ) ;
        }
        
        public function getApprovalStatus()
        {
            $Persetujuan =  PersetujuanSuratKeluar::model()->count(
                                        'id_surat_keluar='.$this->id_surat_keluar
                                    ) ;
            $reject     = $this->getRejectStatus();
            $accept     = $this->getAcceptStatus();
            
            if($Persetujuan==0)
                return "Daftar Persetujuan Kosong";
            elseif($reject>0)
                return 'Di Tolak';
            elseif($Persetujuan==$accept)
                return 'Telah disetujui';
            else
                return 'Menunggu';
        }
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return SuratKeluar the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'surat_keluar';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('no_surat, judul_surat, isi_surat, id_tipe_surat, id_pegawai', 'required'),
			array('draft', 'numerical', 'integerOnly'=>true),
			array('no_surat, judul_surat', 'length', 'max'=>45),
			array('id_tipe_surat, id_pegawai', 'length', 'max'=>20),
			array('tanggal_pembuatan', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id_surat_keluar, no_surat, judul_surat, isi_surat, id_tipe_surat, id_pegawai, tanggal_pembuatan, draft', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
                    'Pegawai' => array(self::BELONGS_TO, 'Pegawai', 'id_pegawai'),
                    'PersetujuanSuratKeluar' => array(self::HAS_MANY, 'PersetujuanSuratKeluar', 'id_surat_keluar'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id_surat_keluar' => 'Id Surat Keluar',
			'no_surat' => 'No Surat',
			'judul_surat' => 'Judul Surat',
			'isi_surat' => 'Isi Surat',
			'id_tipe_surat' => 'Id Tipe Surat',
			'id_pegawai' => 'Id Pegawai',
			'tanggal_pembuatan' => 'Tanggal Pembuatan',
			'draft' => 'Simpan Sebagai Draft',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id_surat_keluar',$this->id_surat_keluar,true);
		$criteria->compare('no_surat',$this->no_surat,true);
		$criteria->compare('judul_surat',$this->judul_surat,true);
		$criteria->compare('isi_surat',$this->isi_surat,true);
		$criteria->compare('id_tipe_surat',$this->id_tipe_surat,true);
		$criteria->compare('tanggal_pembuatan',$this->tanggal_pembuatan,true);
		$criteria->compare('draft',$this->draft);
                
                $criteria->compare('id_pegawai', Yii::app()->user->id, false);
                
                $criteria->order = 'id_surat_keluar DESC';

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}